Lecture03

Stats 422

Setup

# load packages
library(tidyverse)
library(ggthemes)
library(hrbrthemes)
library(ggtech)
library(scales)
library(glue)
library(RColorBrewer)
library(MoMAColors)
library(ggpubr)


# set theme for ggplot2
#ggplot2::theme_set(ggplot2::theme_minimal(base_size = 14))

# set width of code output
options(width = 65)

# set figure parameters for knitr
knitr::opts_chunk$set(
  fig.width = 7, # 7" width
  fig.asp = 0.618, # the golden ratio
  fig.retina = 3, # dpi multiplier for displaying HTML output on retina
  fig.align = "center", # center align figures
  dpi = 300 # higher dpi, sharper image
)

Outline

  • Miscellaneous
    • Installing from GitHub
    • Adding Fonts to your system
    • Working the theme() in ggplot2
    • RColorBrewer
    • ggpubr
  • Break
  • Visualization in Python

data for tonight

load("./LABuldings.RData")
ikea <- read_csv("https://raw.githubusercontent.com/lewv/S24STATS101A/main/data/ikea.csv")

Summaries

# A tibble: 1,675 × 5
# Groups:   POSTAL CODE [122]
   `POSTAL CODE` `PROPERTY TYPE`                   N   freq   pct
           <dbl> <chr>                         <int>  <dbl> <dbl>
 1         90001 Distribution Center              18 0.0968    10
 2         90001 Manufacturing/Industrial Pla…    89 0.478     48
 3         90001 Mixed Use Property                8 0.0430     4
 4         90001 Non-Refrigerated Warehouse       48 0.258     26
 5         90001 Office                            3 0.0161     2
 6         90001 Other                             9 0.0484     5
 7         90001 Refrigerated Warehouse            5 0.0269     3
 8         90001 Self-Storage Facility             6 0.0323     3
 9         90002 Enclosed Mall                     1 0.0227     2
10         90002 Fire Station                     10 0.227     23
# ℹ 1,665 more rows

Summaries

We will keep the largest and reverse sort

# A tibble: 7 × 4
  `POSTAL CODE`     N   freq   pct
          <dbl> <int>  <dbl> <dbl>
1         91311  1639 0.0336     3
2         90045  1371 0.0281     3
3         90028  1318 0.0270     3
4         90020  1210 0.0248     2
5         91402  1109 0.0227     2
6         91406  1099 0.0225     2
7         90036  1004 0.0206     2

Boxplot of the LA data

Modify the Ikea data

ikea4plot <- ikea %>%
  group_by(`category`) %>%
         mutate(N = n(),
                freq = N / sum(N),
                pct = round((freq*100), 0)) %>%
  filter(N > 250) %>%
  ungroup()

Boxplot of the Ikea data

ggplot themes

Basic Themes - B & W

https://ggplot2.tidyverse.org/reference/ggtheme.html

Basic Themes - B & W

https://ggplot2.tidyverse.org/reference/ggtheme.html

box_plot + 
  theme_bw() + 
  theme(axis.text.x = element_text(size = 4))

Basic Themes - Classic

box_plot + 
  theme_classic() + 
  theme(axis.text.x = element_text(size = 6))

Basic Themes - Dark

plot + 
  geom_col(fill = "yellow", color = "black") +
  theme_dark() + 
  theme(axis.text.x = element_text(size = 6))

Basic Themes - Light

plot + 
  theme_light() + 
  theme(axis.text.x = element_text(size = 6))

Basic Themes - Line drawing

plot + 
  theme_linedraw() + 
  theme(axis.text.x = element_text(size = 6))

Basic Themes - Minimal

There is also a blank theme.

plot + 
  theme_minimal() + 
  theme(axis.text.x = element_text(size = 6))

Closer look at theme()

theme_minimal()
List of 136
 $ line                            :List of 6
  ..$ colour       : chr "black"
  ..$ linewidth    : num 0.5
  ..$ linetype     : num 1
  ..$ lineend      : chr "butt"
  ..$ arrow        : logi FALSE
  ..$ inherit.blank: logi TRUE
  ..- attr(*, "class")= chr [1:2] "element_line" "element"
 $ rect                            :List of 5
  ..$ fill         : chr "white"
  ..$ colour       : chr "black"
  ..$ linewidth    : num 0.5
  ..$ linetype     : num 1
  ..$ inherit.blank: logi TRUE
  ..- attr(*, "class")= chr [1:2] "element_rect" "element"
 $ text                            :List of 11
  ..$ family       : chr ""
  ..$ face         : chr "plain"
  ..$ colour       : chr "black"
  ..$ size         : num 11
  ..$ hjust        : num 0.5
  ..$ vjust        : num 0.5
  ..$ angle        : num 0
  ..$ lineheight   : num 0.9
  ..$ margin       : 'margin' num [1:4] 0points 0points 0points 0points
  .. ..- attr(*, "unit")= int 8
  ..$ debug        : logi FALSE
  ..$ inherit.blank: logi TRUE
  ..- attr(*, "class")= chr [1:2] "element_text" "element"
 $ title                           : NULL
 $ aspect.ratio                    : NULL
 $ axis.title                      : NULL
 $ axis.title.x                    :List of 11
  ..$ family       : NULL
  ..$ face         : NULL
  ..$ colour       : NULL
  ..$ size         : NULL
  ..$ hjust        : NULL
  ..$ vjust        : num 1
  ..$ angle        : NULL
  ..$ lineheight   : NULL
  ..$ margin       : 'margin' num [1:4] 2.75points 0points 0points 0points
  .. ..- attr(*, "unit")= int 8
  ..$ debug        : NULL
  ..$ inherit.blank: logi TRUE
  ..- attr(*, "class")= chr [1:2] "element_text" "element"
 $ axis.title.x.top                :List of 11
  ..$ family       : NULL
  ..$ face         : NULL
  ..$ colour       : NULL
  ..$ size         : NULL
  ..$ hjust        : NULL
  ..$ vjust        : num 0
  ..$ angle        : NULL
  ..$ lineheight   : NULL
  ..$ margin       : 'margin' num [1:4] 0points 0points 2.75points 0points
  .. ..- attr(*, "unit")= int 8
  ..$ debug        : NULL
  ..$ inherit.blank: logi TRUE
  ..- attr(*, "class")= chr [1:2] "element_text" "element"
 $ axis.title.x.bottom             : NULL
 $ axis.title.y                    :List of 11
  ..$ family       : NULL
  ..$ face         : NULL
  ..$ colour       : NULL
  ..$ size         : NULL
  ..$ hjust        : NULL
  ..$ vjust        : num 1
  ..$ angle        : num 90
  ..$ lineheight   : NULL
  ..$ margin       : 'margin' num [1:4] 0points 2.75points 0points 0points
  .. ..- attr(*, "unit")= int 8
  ..$ debug        : NULL
  ..$ inherit.blank: logi TRUE
  ..- attr(*, "class")= chr [1:2] "element_text" "element"
 $ axis.title.y.left               : NULL
 $ axis.title.y.right              :List of 11
  ..$ family       : NULL
  ..$ face         : NULL
  ..$ colour       : NULL
  ..$ size         : NULL
  ..$ hjust        : NULL
  ..$ vjust        : num 1
  ..$ angle        : num -90
  ..$ lineheight   : NULL
  ..$ margin       : 'margin' num [1:4] 0points 0points 0points 2.75points
  .. ..- attr(*, "unit")= int 8
  ..$ debug        : NULL
  ..$ inherit.blank: logi TRUE
  ..- attr(*, "class")= chr [1:2] "element_text" "element"
 $ axis.text                       :List of 11
  ..$ family       : NULL
  ..$ face         : NULL
  ..$ colour       : chr "grey30"
  ..$ size         : 'rel' num 0.8
  ..$ hjust        : NULL
  ..$ vjust        : NULL
  ..$ angle        : NULL
  ..$ lineheight   : NULL
  ..$ margin       : NULL
  ..$ debug        : NULL
  ..$ inherit.blank: logi TRUE
  ..- attr(*, "class")= chr [1:2] "element_text" "element"
 $ axis.text.x                     :List of 11
  ..$ family       : NULL
  ..$ face         : NULL
  ..$ colour       : NULL
  ..$ size         : NULL
  ..$ hjust        : NULL
  ..$ vjust        : num 1
  ..$ angle        : NULL
  ..$ lineheight   : NULL
  ..$ margin       : 'margin' num [1:4] 2.2points 0points 0points 0points
  .. ..- attr(*, "unit")= int 8
  ..$ debug        : NULL
  ..$ inherit.blank: logi TRUE
  ..- attr(*, "class")= chr [1:2] "element_text" "element"
 $ axis.text.x.top                 :List of 11
  ..$ family       : NULL
  ..$ face         : NULL
  ..$ colour       : NULL
  ..$ size         : NULL
  ..$ hjust        : NULL
  ..$ vjust        : num 0
  ..$ angle        : NULL
  ..$ lineheight   : NULL
  ..$ margin       : 'margin' num [1:4] 0points 0points 2.2points 0points
  .. ..- attr(*, "unit")= int 8
  ..$ debug        : NULL
  ..$ inherit.blank: logi TRUE
  ..- attr(*, "class")= chr [1:2] "element_text" "element"
 $ axis.text.x.bottom              : NULL
 $ axis.text.y                     :List of 11
  ..$ family       : NULL
  ..$ face         : NULL
  ..$ colour       : NULL
  ..$ size         : NULL
  ..$ hjust        : num 1
  ..$ vjust        : NULL
  ..$ angle        : NULL
  ..$ lineheight   : NULL
  ..$ margin       : 'margin' num [1:4] 0points 2.2points 0points 0points
  .. ..- attr(*, "unit")= int 8
  ..$ debug        : NULL
  ..$ inherit.blank: logi TRUE
  ..- attr(*, "class")= chr [1:2] "element_text" "element"
 $ axis.text.y.left                : NULL
 $ axis.text.y.right               :List of 11
  ..$ family       : NULL
  ..$ face         : NULL
  ..$ colour       : NULL
  ..$ size         : NULL
  ..$ hjust        : num 0
  ..$ vjust        : NULL
  ..$ angle        : NULL
  ..$ lineheight   : NULL
  ..$ margin       : 'margin' num [1:4] 0points 0points 0points 2.2points
  .. ..- attr(*, "unit")= int 8
  ..$ debug        : NULL
  ..$ inherit.blank: logi TRUE
  ..- attr(*, "class")= chr [1:2] "element_text" "element"
 $ axis.text.theta                 : NULL
 $ axis.text.r                     :List of 11
  ..$ family       : NULL
  ..$ face         : NULL
  ..$ colour       : NULL
  ..$ size         : NULL
  ..$ hjust        : num 0.5
  ..$ vjust        : NULL
  ..$ angle        : NULL
  ..$ lineheight   : NULL
  ..$ margin       : 'margin' num [1:4] 0points 2.2points 0points 2.2points
  .. ..- attr(*, "unit")= int 8
  ..$ debug        : NULL
  ..$ inherit.blank: logi TRUE
  ..- attr(*, "class")= chr [1:2] "element_text" "element"
 $ axis.ticks                      : list()
  ..- attr(*, "class")= chr [1:2] "element_blank" "element"
 $ axis.ticks.x                    : NULL
 $ axis.ticks.x.top                : NULL
 $ axis.ticks.x.bottom             : NULL
 $ axis.ticks.y                    : NULL
 $ axis.ticks.y.left               : NULL
 $ axis.ticks.y.right              : NULL
 $ axis.ticks.theta                : NULL
 $ axis.ticks.r                    : NULL
 $ axis.minor.ticks.x.top          : NULL
 $ axis.minor.ticks.x.bottom       : NULL
 $ axis.minor.ticks.y.left         : NULL
 $ axis.minor.ticks.y.right        : NULL
 $ axis.minor.ticks.theta          : NULL
 $ axis.minor.ticks.r              : NULL
 $ axis.ticks.length               : 'simpleUnit' num 2.75points
  ..- attr(*, "unit")= int 8
 $ axis.ticks.length.x             : NULL
 $ axis.ticks.length.x.top         : NULL
 $ axis.ticks.length.x.bottom      : NULL
 $ axis.ticks.length.y             : NULL
 $ axis.ticks.length.y.left        : NULL
 $ axis.ticks.length.y.right       : NULL
 $ axis.ticks.length.theta         : NULL
 $ axis.ticks.length.r             : NULL
 $ axis.minor.ticks.length         : 'rel' num 0.75
 $ axis.minor.ticks.length.x       : NULL
 $ axis.minor.ticks.length.x.top   : NULL
 $ axis.minor.ticks.length.x.bottom: NULL
 $ axis.minor.ticks.length.y       : NULL
 $ axis.minor.ticks.length.y.left  : NULL
 $ axis.minor.ticks.length.y.right : NULL
 $ axis.minor.ticks.length.theta   : NULL
 $ axis.minor.ticks.length.r       : NULL
 $ axis.line                       : list()
  ..- attr(*, "class")= chr [1:2] "element_blank" "element"
 $ axis.line.x                     : NULL
 $ axis.line.x.top                 : NULL
 $ axis.line.x.bottom              : NULL
 $ axis.line.y                     : NULL
 $ axis.line.y.left                : NULL
 $ axis.line.y.right               : NULL
 $ axis.line.theta                 : NULL
 $ axis.line.r                     : NULL
 $ legend.background               : list()
  ..- attr(*, "class")= chr [1:2] "element_blank" "element"
 $ legend.margin                   : 'margin' num [1:4] 5.5points 5.5points 5.5points 5.5points
  ..- attr(*, "unit")= int 8
 $ legend.spacing                  : 'simpleUnit' num 11points
  ..- attr(*, "unit")= int 8
 $ legend.spacing.x                : NULL
 $ legend.spacing.y                : NULL
 $ legend.key                      : list()
  ..- attr(*, "class")= chr [1:2] "element_blank" "element"
 $ legend.key.size                 : 'simpleUnit' num 1.2lines
  ..- attr(*, "unit")= int 3
 $ legend.key.height               : NULL
 $ legend.key.width                : NULL
 $ legend.key.spacing              : 'simpleUnit' num 5.5points
  ..- attr(*, "unit")= int 8
 $ legend.key.spacing.x            : NULL
 $ legend.key.spacing.y            : NULL
 $ legend.frame                    : NULL
 $ legend.ticks                    : NULL
 $ legend.ticks.length             : 'rel' num 0.2
 $ legend.axis.line                : NULL
 $ legend.text                     :List of 11
  ..$ family       : NULL
  ..$ face         : NULL
  ..$ colour       : NULL
  ..$ size         : 'rel' num 0.8
  ..$ hjust        : NULL
  ..$ vjust        : NULL
  ..$ angle        : NULL
  ..$ lineheight   : NULL
  ..$ margin       : NULL
  ..$ debug        : NULL
  ..$ inherit.blank: logi TRUE
  ..- attr(*, "class")= chr [1:2] "element_text" "element"
 $ legend.text.position            : NULL
 $ legend.title                    :List of 11
  ..$ family       : NULL
  ..$ face         : NULL
  ..$ colour       : NULL
  ..$ size         : NULL
  ..$ hjust        : num 0
  ..$ vjust        : NULL
  ..$ angle        : NULL
  ..$ lineheight   : NULL
  ..$ margin       : NULL
  ..$ debug        : NULL
  ..$ inherit.blank: logi TRUE
  ..- attr(*, "class")= chr [1:2] "element_text" "element"
 $ legend.title.position           : NULL
 $ legend.position                 : chr "right"
 $ legend.position.inside          : NULL
 $ legend.direction                : NULL
 $ legend.byrow                    : NULL
 $ legend.justification            : chr "center"
 $ legend.justification.top        : NULL
 $ legend.justification.bottom     : NULL
 $ legend.justification.left       : NULL
 $ legend.justification.right      : NULL
 $ legend.justification.inside     : NULL
 $ legend.location                 : NULL
 $ legend.box                      : NULL
 $ legend.box.just                 : NULL
 $ legend.box.margin               : 'margin' num [1:4] 0cm 0cm 0cm 0cm
  ..- attr(*, "unit")= int 1
 $ legend.box.background           : list()
  ..- attr(*, "class")= chr [1:2] "element_blank" "element"
 $ legend.box.spacing              : 'simpleUnit' num 11points
  ..- attr(*, "unit")= int 8
  [list output truncated]
 - attr(*, "class")= chr [1:2] "theme" "gg"
 - attr(*, "complete")= logi TRUE
 - attr(*, "validate")= logi TRUE

ggthemes

ggthemes

  • You can create your own themes

  • Or use some of the popular created themes

  • and these can be modified

ggthemes - base

# library(ggthemes)
plot + 
  theme_base() +  # like base R
  theme(axis.text.x = element_text(size = 6))

ggthemes - calc

plot + 
  theme_calc() +  # like Free Office
  theme(axis.text.x = element_text(size = 6))

ggthemes - clean

plot + 
  theme_clean() +  # like minimal
  theme(axis.text.x = element_text(size = 6))

ggthemes - The Economist

plot + 
  theme_economist() +  # mimic The Economist publication
  theme(axis.text.x = element_text(size = 6))

ggthemes - The Economist white

plot + 
  theme_economist_white() +  # mimic The Economist publication
  theme(axis.text.x = element_text(size = 6))

ggthemes - examination

function (base_size = 10, base_family = "sans", horizontal = TRUE, 
    dkpanel = FALSE) 
{
    bgcolors <- deframe(ggthemes::ggthemes_data[["economist"]][["bg"]])
    ret <- theme_foundation(base_size = base_size, base_family = base_family) + 
        theme(line = element_line(colour = "black"), rect = element_rect(fill = bgcolors["ebg"], 
            colour = NA, linetype = 1), text = element_text(colour = "black"), 
            axis.line = element_line(linewidth = rel(0.8)), axis.line.y = element_blank(), 
            axis.text = element_text(size = rel(1)), axis.text.x = element_text(vjust = 0, 
                margin = margin(t = base_size, unit = "pt")), 
            axis.text.x.top = element_text(vjust = 0, margin = margin(b = base_size, 
                unit = "pt")), axis.text.y = element_text(hjust = 0, 
                margin = margin(r = base_size, unit = "pt")), 
            axis.ticks = element_line(), axis.ticks.y = element_blank(), 
            axis.title = element_text(size = rel(1)), axis.title.x = element_text(), 
            axis.title.y = element_text(angle = 90), axis.ticks.length = unit(-base_size * 
                0.5, "points"), legend.background = element_rect(linetype = 0), 
            legend.spacing = unit(base_size * 1.5, "points"), 
            legend.key = element_rect(linetype = 0), legend.key.size = unit(1.2, 
                "lines"), legend.key.height = NULL, legend.key.width = NULL, 
            legend.text = element_text(size = rel(1.25)), legend.title = element_text(size = rel(1), 
                hjust = 0), legend.position = "top", legend.direction = NULL, 
            legend.justification = "center", panel.background = element_rect(linetype = 0), 
            panel.border = element_blank(), panel.grid.major = element_line(colour = "white", 
                linewidth = rel(1.75)), panel.grid.minor = element_blank(), 
            panel.spacing = unit(0.25, "lines"), strip.background = element_rect(fill = bgcolors["ebg"], 
                colour = NA, linetype = 0), strip.text = element_text(size = rel(1.25)), 
            strip.text.x = element_text(), strip.text.y = element_text(angle = -90), 
            plot.background = element_rect(fill = bgcolors["blue-gray"], 
                colour = NA), plot.title = element_text(size = rel(1.5), 
                hjust = 0, face = "bold"), plot.margin = unit(c(6, 
                5, 6, 5) * 2, "points"), complete = TRUE)
    if (horizontal) {
        ret <- ret + theme(panel.grid.major.x = element_blank())
    }
    else {
        ret <- ret + theme(panel.grid.major.y = element_blank())
    }
    if (dkpanel == TRUE) {
        ret <- ret + theme(panel.background = element_rect(fill = unname(bgcolors["dark blue-gray"])), 
            strip.background = element_rect(fill = unname(bgcolors["dark blue-gray"])))
    }
    ret
}
<bytecode: 0x144448270>
<environment: namespace:ggthemes>

ggthemes - Excel (new)

plot + 
  theme_excel_new() +  # for fun
  theme(axis.text.x = element_text(size = 6))

ggthemes - Few (Stephen)

plot + 
  theme_few() +  # applies Few's rules
  theme(axis.text.x = element_text(size = 6)) 

ggthemes - fivethirtyeight

plot + 
  theme_fivethirtyeight() +  # inspired by 
  theme(axis.text.x = element_text(size = 8))

ggthemes - Google Doc

plot + 
  theme_gdocs() +  # Google Doc's charts
  theme(axis.text.x = element_text(size = 6))

ggthemes - Highchart

plot + 
  theme_hc() +  # Highcharts
  theme(axis.text.x = element_text(size = 8))

ggthemes - inverse

plot + 
  theme_igray() +  # inverse
  theme(axis.text.x = element_text(size = 6))

ggthemes - pander

plot + 
  theme_pander() +  # pander default
  theme(axis.text.x = element_text(size = 6))

ggthemes - classic base R

plot + 
  theme_par()  # classic Base r plot

ggthemes - solarized palette

plot + 
  theme_solarized() +  # solarized palette
  theme(axis.text.x = element_text(size = 7))

ggthemes - solarized variant

plot + 
  theme_solarized_2() +  
  theme(axis.text.x = element_text(size = 7))

ggthemes - Stata

plot + 
  theme_stata() +  
  theme(axis.text.x = element_text(size = 6))

ggthemes - Tufte

plot + 
  theme_tufte() +  # based on Tufte's designs
  theme(axis.text.x = element_text(size = 8))

ggthemes - Wall Street Journal

plot + 
  theme_wsj() + 
  theme(axis.text.x = element_text(size = 7))

hrbrthemes

hrbrthemes - Financial Times

plot + 
  theme_ft_rc() +  #Dark theme based on FT’s dark theme
  theme(axis.text.x = element_text(size = 8))

hrbrthemes - Arial (changed font)

plot + 
  theme_ipsum() # Arial Narrow

hrbrthemes - Goldman Sachs (download, install)

plot + 
  theme_ipsum_gs() # Goldman Sachs Sans
# May need to install - https://design.gs.com/foundation/typography/goldman-sans

plot + 
  theme_modern_rc() 
  # Modern Roboto Condensed - May need to install font

ggtech - installing from Github

## https://exts.ggplot2.tidyverse.org/ggtech.html
## run once
## devtools::install_github("ricardo-bion/ggtech", 
##                          dependencies=TRUE)

installing fonts in R

library(extrafont)

## Facebook 
download.file("http://social-fonts.com/assets/fonts/facebook-letter-faces/facebook-letter-faces.ttf", "/Library/Fonts/facebook-letter-faces.ttf", method="curl")

font_import(pattern = 'facebook-letter-faces.ttf', prompt=FALSE)

## Google 
download.file("http://social-fonts.com/assets/fonts/product-sans/product-sans.ttf", "/Library/Fonts/product-sans.ttf", method="curl")

font_import(pattern = 'product-sans.ttf', prompt=FALSE)

## Twitter 
download.file("http://social-fonts.com/assets/fonts/pico-black/pico-black.ttf", "/Library/Fonts/pico-black.ttf", method="curl")

download.file("http://social-fonts.com/assets/fonts/arista-light/arista-light.ttf", "/Library/Fonts/arista-light.ttf", method="curl")

font_import(pattern = 'pico-black.ttf', prompt=FALSE)
font_import(pattern = 'arista-light.ttf', prompt=FALSE)

ggtech: Facebook

library(ggtech)
plot + 
  theme_tech(theme = "facebook") 

ggtech: Google

plot + 
  theme_tech(theme = "google") 

ggtech: Twitter

plot + 
  theme_tech(theme = "twitter") 

RColorBrewer and Other Color Additions

RColorBrewer

  • is a package of palettes for making graphics that are visually appealing
  • the palettes were created to address common visualization problems
  • the palettes properly optimized for readability/accessiblity
  • pretty + functional
  • integrates with ggplot2
  • ColorBrewer is also in Python

RColorBrewer

Has palettes for different kinds of data:

  • Sequential palettes are for data that has ordering

  • Diverging palettes are for data where the extremes are important and data deviates around a center (e.g., baseline temperature or health measure).

  • Qualitative palettes for categorical data

palettes

display.brewer.all()

## ### Example Application

ggplot(data=iris, aes(x=Species, y=Petal.Length, fill=Species)) +
  geom_boxplot() +
  scale_fill_brewer(palette = "BrBG") 

Extract colors

installing from GitHub

# install.packages("devtools")
devtools::install_github("BlakeRMills/MoMAColors")

MoMA colors - Example with fill

ggplot(data=iris, aes(x=Species, y=Petal.Length, fill=Species)) +
  geom_violin() +
  scale_fill_manual(values=moma.colors("Connors", 3))

MoMA colors - Example with color discrete

ggplot(data=iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) +
  geom_point(size=2) +
  scale_color_moma_d("Warhol") # discrete

MoMA colors - Example with color continuous

ggplot(data=iris, aes(x=Species, y=Sepal.Width, color=Sepal.Width)) +
  geom_point(size=3) +
  scale_color_gradientn(colors=moma.colors("Flash"))

Add your own colors

ucla_colors <- c("#2774AE","#FFD100","#DAEBFE")

ggplot(data=iris, aes(x=Species, y=Petal.Length, fill=Species)) +
  geom_violin() +
  scale_fill_manual(values=ucla_colors)

library(ggpubr)

ggpubr - enhancements for publication

  • Perhaps you want several plots on a single figure

  • Or slightly nicer basic statistical graphics with less work!

gghistogram

gghistogram(ikea4plot, x = "price",
   add = "mean", rug = TRUE,
   color = "other_colors", fill = "other_colors", bins= 15)

ggboxplot

my_comp <- list(c("setosa", "versicolor"), c("versicolor", "virginica"), c("setosa", "virginica"))

ggviolin(iris, x = "Species", y = "Sepal.Length", fill = "Species",
         add = "boxplot", add.params = list(fill = "white"))+
   stat_compare_means(comparisons = my_comp) +
   stat_compare_means(label.y = 7)

ggarrange

Suppose I have two saved ggplots, they can exist in the same graphic:

ggarrange(ggplot1, ggplot2, ncol = 2)

BREAK